package com.dc.common.aop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

//import com.dc.system.mapper.RoleMapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.dc.base.exception.BusinessException;
import com.dc.common.constants.ConstantsCommon;
import com.dc.common.em.EnumOperationModule;
import com.dc.common.model.BaseModel;
import com.dc.system.model.SysRolePermission;
import com.dc.system.model.SysUser;

/**
 * 权限验证切面
 */
@Component
@Aspect
public class AuthorityAspect {

    /*@Autowired
    private RoleMapper roleMapper;

    @Pointcut("@annotation(com.dc.common.aop.AuthorityAop)")
    public void authorityAspect() {
    }

    @Before("authorityAspect() && @annotation(data)")
    public void doBefore(JoinPoint point, AuthorityAop data) throws Throwable {
        // 获取request以及seesion
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
//        // 获取baseModel
//        BaseModel baseModel = new BaseModel();
//
//        if (point.getArgs() != null) {
//            for (Object obj : point.getArgs()) {
//                if (obj != null && obj.getClass() == BaseModel.class) {
//                    baseModel = (BaseModel) obj;
//                    break;
//                }
//            }
//        }
        // 获得当前用户
        SysUser systemUser = (SysUser) session.getAttribute(ConstantsCommon.LOGIN_INFO);
        if (systemUser == null) {
            throw new BusinessException(9, "登录超时，请重新登陆");
        } else {
            //判断权限
            String type = data.type();
            int code = data.module().getCode();
            int num = EnumOperationModule.getNumByCodeType(data.module().getCode(), data.type());
            //根据用户id和模块code去查询权限
            Map map = new HashMap();
            map.put("userId", systemUser.getUserId());
            map.put("code", code);
            //权限值取出后存session，下一次从session中取，如未取到，则查询数据库，并赋值到session
            String permissionValue = "";
            if (session.getAttribute(code + "") != null && !"".equals(session.getAttribute(code + ""))) {
                permissionValue = (String) session.getAttribute(code + "");
            } else {
                SysRolePermission sysRolePermission = roleMapper.getRolePermissionByUserIdAndCode(map);
                permissionValue = sysRolePermission.getPermissionValue();
                session.setAttribute(code + "", sysRolePermission.getPermissionValue());
            }
            if (permissionValue != null && !"".equals(permissionValue) && permissionValue.charAt(num) == '1') {

            } else {
                throw new BusinessException(8, "对不起，你没有权限！");
            }
        }

    }*/
}
